package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Comment;
import cn.wolfcode.trip.base.domain.DoGood;
import cn.wolfcode.trip.base.domain.Dynamic;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.CommentMapper;
import cn.wolfcode.trip.base.mapper.DoGoodMapper;
import cn.wolfcode.trip.base.mapper.DynamicMapper;
import cn.wolfcode.trip.base.query.CommentQueryObject;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.DynamicQueryObject;
import cn.wolfcode.trip.base.service.IDynamicService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DynamicServiceImpl implements IDynamicService {
    @Autowired
    private DynamicMapper dynamicMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DoGoodMapper doGoodMapper;

    @Autowired
    private CommentMapper commentMapper;
    @Override
    public void save(Dynamic dynamic) {
        User user = UserContext.getCurrentUser();
        dynamic.setUser(user);
        dynamic.setCreateTime(new Date());
        dynamicMapper.insert(dynamic);
    }

    public PageInfo queryDynamicById(DynamicQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Dynamic> list = dynamicMapper.selectForDynamicList(qo);
        CommentQueryObject queryObject = new CommentQueryObject();
        queryObject.setType(5);
        for (Dynamic dynamic : list) {
            queryObject.setAnswer_id(dynamic.getId());
            List<Comment> list1 = commentMapper.selectForList(queryObject);
            dynamic.setComment(list1);

            //通过朋友圈动态id查询点过赞的用户
            List<User> users = userMapper.selectdoGoodCountForDynamicId(dynamic.getId());
            dynamic.setUsers(users);
            DoGood doGood = doGoodMapper.selectByOne(new DoGood(qo.getLoginId(), dynamic.getId(), 4));
            if (doGood == null) {
                dynamic.setState(0);
            } else {
                dynamic.setState(1);
            }
        }
        return new PageInfo(list);
    }

    public PageInfo queryEveryPage(DynamicQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Dynamic> list = dynamicMapper.selectForseleEveryPage(qo);
        CommentQueryObject queryObject = new CommentQueryObject();
        queryObject.setType(5);
        for (Dynamic dynamic : list) {
            queryObject.setAnswer_id(dynamic.getId());
            List<Comment> list1 = commentMapper.selectForList(queryObject);
            dynamic.setComment(list1);

            //通过朋友圈动态id查询点过赞的用户
            List<User> users = userMapper.selectdoGoodCountForDynamicId(dynamic.getId());
            dynamic.setUsers(users);
            DoGood doGood = doGoodMapper.selectByOne(new DoGood(qo.getUserId(), dynamic.getId(), 4));
            if (doGood != null) {
                dynamic.setState(1);
            }else{
                dynamic.setState(0);
            }
        }
        return new PageInfo(list);
    }

    @Override
    public void deleteById(Long id) {
        dynamicMapper.deleteByPrimaryKey(id);
    }

}
